local sk__shenyin = fk.CreateSkill {

  name = "sk__shenyin",

  tags = { Skill.Compulsory, },

}



sk__shenyin:addEffect(fk.GameStart, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__shenyin.name) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@sk__shenyin", 1)
    local skills = table.map(table.filter(player.player_skills, function(s)
      return s:isPlayerSkill(player) and s.visible
    end), function(s)
      return s.name
    end)
    room:setPlayerMark(player, sk__shenyin.name, { player.hp, player.maxHp, skills,
      player:getTableMark("sk__yingshi"), player:getTableMark("@$sk__langxi") })
  end,
})
sk__shenyin:addEffect(fk.Deathed, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__shenyin.name) then
      return data.damage and data.damage.from == player
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@sk__shenyin", 1)
  end,
})
sk__shenyin:addEffect(fk.TurnStart, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__shenyin.name) then
      return target == player and player:getMark("@sk__shenyin") > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local prompt = "#sk__shenyin-record1"
    if player:getMark(sk__shenyin.name) ~= 0 then
      prompt = "#sk__shenyin-record2:::" ..
          player:getMark(sk__shenyin.name)[1] .. ":" .. player:getMark(sk__shenyin.name)[2]
    end
    if room:askForSkillInvoke(player, sk__shenyin.name, nil, prompt) then
      local skills = table.map(table.filter(player.player_skills, function(s)
        return s:isPlayerSkill(player) and s.visible
      end), function(s)
        return s.name
      end)
      room:setPlayerMark(player, sk__shenyin.name, { player.hp, player.maxHp, skills,
        player:getTableMark("sk__yingshi"), player:getTableMark("@$sk__langxi") })
      room:addPlayerMark(player, "@sk__shenyin", 1)
    end
  end,
})
sk__shenyin:addEffect(fk.EnterDying, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__shenyin.name) then
      return target == player and player:getMark(sk__shenyin.name) ~= 0 and player:getMark("@sk__shenyin") > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local record = player:getMark(sk__shenyin.name)
    local prompt = "#sk__shenyin-load:::" .. record[1] .. ":" .. record[2]
    if room:askForSkillInvoke(player, sk__shenyin.name, nil, prompt) then
      player.maxHp = record[2]
      room:broadcastProperty(player, "maxHp")
      player.hp = record[1]
      room:broadcastProperty(player, "hp")
      local skills = {}
      for _, skill in ipairs(record[3]) do
        if not player:hasSkill(skill, true) then
          table.insert(skills, skill)
        end
      end
      for _, skill in ipairs(player.player_skills) do
        if skill:isPlayerSkill(player) and skill.visible and not table.contains(record[3], skill.name) then
          table.insert(skills, "-" .. skill.name)
        end
      end
      room:handleAddLoseSkills(player, skills, nil, true, false)
      room:setPlayerMark(player, "sk__yingshi", record[4])
      local mark = player:getTableMark("sk__yingshi")
      if #mark > 0 then
        for _, id in ipairs(Fk:getAllCardIds()) do
          if table.find(room.alive_players, function(p)
                return table.contains(p:getTableMark(sk__shenyin.name), id)
              end) then
            room:setCardMark(Fk:getCardById(id), "@@sk__yingshi", 1)
          else
            room:setCardMark(Fk:getCardById(id), "@@sk__yingshi", 0)
          end
        end
      end
      room:setPlayerMark(player, "@$sk__langxi", record[5])
      if player.dead then return end
      player:drawCards(2 * player:getMark("@sk__shenyin"), sk__shenyin.name)
      room:setPlayerMark(player, "@sk__shenyin", 0)
    end
  end,
})

sk__shenyin:addLoseEffect(function(self, player, is_death)
  local room = player.room
  if player:hasSkill(sk__shenyin.name) and player:getMark(sk__shenyin.name) ~= 0 and
      player:getMark("@sk__shenyin") > 0 then
    local record = player:getMark(sk__shenyin.name)
    local prompt = "#sk__shenyin-load:::" .. record[1] .. ":" .. record[2]
    if room:askForSkillInvoke(player, sk__shenyin.name, nil, prompt) then
      player.maxHp = record[2]
      room:broadcastProperty(player, "maxHp")
      player.hp = record[1]
      room:broadcastProperty(player, "hp")
      local skills = {}
      for _, skill in ipairs(record[3]) do
        if not player:hasSkill(skill, true) then
          table.insert(skills, skill)
        end
      end
      for _, skill in ipairs(player.player_skills) do
        if skill:isPlayerSkill(player) and skill.visible and not table.contains(record[3], skill.name) then
          table.insert(skills, "-" .. skill.name)
        end
      end
      room:handleAddLoseSkills(player, skills, nil, true, false)
      room:setPlayerMark(player, "sk__yingshi", record[4])
      local mark = player:getTableMark("sk__yingshi")
      if #mark > 0 then
        for _, id in ipairs(Fk:getAllCardIds()) do
          if table.find(room.alive_players, function(p)
                return table.contains(p:getTableMark(sk__shenyin.name), id)
              end) then
            room:setCardMark(Fk:getCardById(id), "@@sk__yingshi", 1)
          else
            room:setCardMark(Fk:getCardById(id), "@@sk__yingshi", 0)
          end
        end
      end
      room:setPlayerMark(player, "@$sk__langxi", record[5])
      if player.dead then return end
      player:drawCards(2 * player:getMark("@sk__shenyin"), sk__shenyin.name)
      room:setPlayerMark(player, "@sk__shenyin", 0)
    end
  end
end)


return sk__shenyin
